package com.xgpro.mobile.talentintroduction.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xgpro.common.enums.ResultCodeEnum;
import com.xgpro.common.exceptions.BusinessRuntimeException;
import com.xgpro.common.service.BaseService;
import com.xgpro.common.utils.FileTool;
import com.xgpro.common.utils.ReadPropertiesUtil;
import com.xgpro.domain.talentintroduction.dto.UserMessage;
import com.xgpro.domain.talentintroduction.entity.IntroduceJob;
import com.xgpro.domain.talentintroduction.entity.Recruitment;
import com.xgpro.domain.talentintroduction.entity.User;
import com.xgpro.domain.talentintroduction.vo.*;
import com.xgpro.mapper.talentintroduction.IntroduceJobMapper;
import com.xgpro.mapper.talentintroduction.RecruitmentMapper;
import com.xgpro.mapper.talentintroduction.UserMapper;
import com.xgpro.mobile.talentintroduction.service.RecruitmentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author tx
 * @version 1.0
 * @date 2019/8/18 20:09
 */
@Service
@Transactional(readOnly = false)
public class RecruitmentServiceImpl extends BaseService implements RecruitmentService {
    /**
     * 招聘信息删除状态
     */
    private static final Integer RECR_FLAG=2;

    @Resource
    private RecruitmentMapper recruitmentMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private IntroduceJobMapper introduceJobMapper;

    /**
     * 查询招聘列表
     *
     * @param recruitmentFront
     * @return
     */
    @Override
    public RecruitmentBackResult qryRecruitmentList(RecruitmentFront recruitmentFront) {
        RecruitmentBackResult recruitmentBackResult = new RecruitmentBackResult();
        Example example = new Example(Recruitment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("flag", 1);
        if (recruitmentFront != null && !"".equals(recruitmentFront)) {
            criteria.andEqualTo("kind",recruitmentFront.getKind());
            if (recruitmentFront.getEdu() != null && recruitmentFront.getEdu() != null) {
                criteria.andEqualTo("edu",recruitmentFront.getEdu());
            }
            if (recruitmentFront.getCity() != null && !"".equals(recruitmentFront.getCity())) {
                criteria.andLike("city","%"+recruitmentFront.getCity()+"%");
            }
            if (recruitmentFront.getSalary() != null&&recruitmentFront.getSalary()!=0) {
                criteria.andLessThanOrEqualTo("fSalary", recruitmentFront.getSalary());
            }
            if (recruitmentFront.getSalary() != null&&recruitmentFront.getSalary()!=0) {
                criteria.andGreaterThanOrEqualTo("eSalary", recruitmentFront.getSalary());
            }
            if (recruitmentFront.getRecommended() != null&&recruitmentFront.getRecommended()!=0) {
                criteria.andEqualTo("recommended", recruitmentFront.getRecommended());
            }
        }
        example.setOrderByClause("update_time desc");
        PageHelper.startPage(recruitmentFront.getPage(),recruitmentFront.getPageSize());
        Page<Recruitment> pages = (Page<Recruitment>) recruitmentMapper.selectByExample(example);
        recruitmentBackResult.setTotals(pages.getTotal());
        List<Recruitment> recruitmentList =pages.getResult();
        if (CollectionUtils.isEmpty(recruitmentList)) {
            throw new BusinessRuntimeException(ResultCodeEnum.NO_RECRUITMENT.getCode(), ResultCodeEnum.NO_RECRUITMENT.getMessage());
        }
        List<Integer> ids=new ArrayList<>();
        for (Recruitment recruitment : recruitmentList) {
            ids.add(recruitment.getUserId());
        }
        List<UserMessage> userMessages = userMapper.selectByList(ids);
        List<RecruitmentBack> list=new ArrayList<>();
        RecruitmentBack recruitmentBack=null;
        for (Recruitment recruitment : recruitmentList) {
            for (UserMessage userMessage : userMessages) {
                if(userMessage.getId().equals(recruitment.getUserId())){
                    recruitmentBack=new RecruitmentBack();
                    recruitmentBack.setRecruitment(recruitment);
                    recruitmentBack.setName(userMessage.getUsername());
                    recruitmentBack.setPhoto(userMessage.getPhoto());
                    recruitmentBack.setDeptname(userMessage.getDeptname());
                    list.add(recruitmentBack);
                    break;
                }
            }
        }
        recruitmentBackResult.setRecruitmentBacks(list);
        return recruitmentBackResult;
    }

    /**
     * 招聘详情展示
     * @param id
     * @return
     */
    @Override
    public Recruitment qryRecruitmentById(Integer id) {
        Recruitment recruitment = recruitmentMapper.selectByPrimaryKey(id);
        if(recruitment==null||RECR_FLAG.equals(recruitment.getFlag())){
            throw new BusinessRuntimeException(ResultCodeEnum.DE_RECRUITMENT.getCode(), ResultCodeEnum.DE_RECRUITMENT.getMessage());
        }
        return recruitment;
    }

    /**
     * 回显人才引荐的基本信息
     * @param recrId
     * @param userId
     * @return
     */
    @Override
    public UserInfo qryUserInfoByRecruitmentId(Integer recrId, Integer userId) {
        Recruitment recruitment = recruitmentMapper.selectByPrimaryKey(recrId);
        if(recruitment==null){
            throw new BusinessRuntimeException(ResultCodeEnum.DE_RECRUITMENT.getCode(), ResultCodeEnum.DE_RECRUITMENT.getMessage());
        }
        UserInfo userInfo=new UserInfo();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        String time=simpleDateFormat.format(new Date());
        userInfo.setTime(time);
        userInfo.setRecruitmentName(recruitment.getName());
        userInfo.setKind(recruitment.getKind());
            UserInfo userInfoFromDb = userMapper.selectById(userId);
            userInfo.setName(userInfoFromDb.getName());
            String[] split =null;
            if (null != userInfoFromDb.getPhone() && ""!= userInfoFromDb.getPhone()){
                split = userInfoFromDb.getPhone().split("-");
            }

            if (split.length > 1 ){
                userInfo.setPhone(split[1]);
            }else {
                userInfo.setPhone(userInfoFromDb.getPhone());
            }

            userInfo.setEdu(userInfoFromDb.getEdu());
            userInfo.setJob(userInfoFromDb.getJob());
            userInfo.setRankName(userInfoFromDb.getRankName());
            userInfo.setJobTime(userInfoFromDb.getJobTime());
            return userInfo;
    }

    /**
     * 竞聘岗位人员信息添加
     * @param introduceJob
     * @param id
     * @return
     */
    @Override
    public Integer addIntroduceJob(IntroduceJob introduceJob,Integer id) {
        introduceJob.setIntroduceUserId(id);
        introduceJob.setTime(new Date());
        int result = introduceJobMapper.insertSelective(introduceJob);
        return result;
    }




}
